package com.zt.controller;

import com.zt.common.Result;
import com.zt.service.FileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器
 * @author 周明震
 * @date 2025/7/21
 */
@RestController
@RequestMapping("/upload")
@Slf4j
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 上传科目图标
     */
    @PostMapping("/subject-icon")
    public Result<String> uploadSubjectIcon(@RequestParam("file") MultipartFile file) {
        try {
            String imageUrl = fileUploadService.uploadSubjectIcon(file);
            return Result.success("上传成功", imageUrl);
        } catch (Exception e) {
            log.error("科目图标上传失败: {}", e.getMessage(), e);
            return Result.error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传头像
     */
    @PostMapping("/avatar")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        try {
            String imageUrl = fileUploadService.uploadAvatar(file);
            return Result.success("上传成功", imageUrl);
        } catch (Exception e) {
            log.error("头像上传失败: {}", e.getMessage(), e);
            return Result.error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传课程封面
     */
    @PostMapping("/course-cover")
    public Result<String> uploadCourseCover(@RequestParam("file") MultipartFile file) {
        try {
            String imageUrl = fileUploadService.uploadCourseCover(file);
            return Result.success("上传成功", imageUrl);
        } catch (Exception e) {
            log.error("课程封面上传失败: {}", e.getMessage(), e);
            return Result.error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传教师头像
     */
    @PostMapping("/teacher-avatar")
    public Result<String> uploadTeacherAvatar(@RequestParam("file") MultipartFile file) {
        try {
            String imageUrl = fileUploadService.uploadTeacherAvatar(file);
            return Result.success("上传成功", imageUrl);
        } catch (Exception e) {
            log.error("教师头像上传失败: {}", e.getMessage(), e);
            return Result.error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传教师证书
     */
    @PostMapping("/teacher-certificate")
    public Result<String> uploadTeacherCertificate(@RequestParam("file") MultipartFile file) {
        try {
            String imageUrl = fileUploadService.uploadTeacherCertificate(file);
            return Result.success("上传成功", imageUrl);
        } catch (Exception e) {
            log.error("教师证书上传失败: {}", e.getMessage(), e);
            return Result.error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 通用文件上传
     */
    @PostMapping("/file")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file,
                           @RequestParam(value = "folder", defaultValue = "common") String folder) {
        try {
            // 根据文件夹类型选择合适的上传方法
            String imageUrl;
            switch (folder.toLowerCase()) {
                case "course-covers":
                    imageUrl = fileUploadService.uploadCourseCover(file);
                    break;
                case "teacher-avatars":
                    imageUrl = fileUploadService.uploadTeacherAvatar(file);
                    break;
                case "teacher-certificates":
                    imageUrl = fileUploadService.uploadTeacherCertificate(file);
                    break;
                case "avatars":
                    imageUrl = fileUploadService.uploadAvatar(file);
                    break;
                case "subject-icons":
                    imageUrl = fileUploadService.uploadSubjectIcon(file);
                    break;
                default:
                    // 通用上传，使用课程封面的限制
                    imageUrl = fileUploadService.uploadCourseCover(file);
                    break;
            }
            return Result.success("上传成功", imageUrl);
        } catch (Exception e) {
            log.error("文件上传失败: {}", e.getMessage(), e);
            return Result.error("上传失败：" + e.getMessage());
        }
    }
}
